<template>
  <div id="app" v-loading="loading">
    <v-form-render
      ref="vFormRef"
      :form-json="formJson"
      :form-data="formData"
      :option-data="optionData"
    />
    <div v-if="showBtn" class="footerBtn">
      <el-button @click="closeForm">取消</el-button>
      <el-button type="primary" @click="submitForm">保存</el-button>
    </div>
  </div>
</template>
<script>
import { requestMethod, requestAll } from '@/api/system/desform/onlineForm/onlCgformHead'
import { getDesignFormData } from '@/api/system/desform/data/designFormData'
import { getToken } from '@/utils/auth'

  export default {
    data() {
      return {
        formJson: {},
        formData: {},
        optionData: {},
        pojo: {},
        showBtn: true,
        loading: false,
        // 是否关联 项目 或者基金
        isRelevance: false,
         // 请求对象
        requestPojo: {
            getAutoFormItem: '/head/OnlineApiTest/api/getFormItem/',
            postAutoForm: '',
            getListDetailsData: '/head/OnlineApiTest/api/form/'
        },
        headId: '',
        data: {},
        rowData: null
      }
    },
     mounted() {
    this.$bus.$emit('async_done')
    window.token = getToken()
    window.baseUrl = process.env.VUE_APP_BASE_API
    window.currentLoginUserId = this.$store.state.user.userId
    window.userTree = this.$store.state.user.userTree
    window.initProperty = {
      deal_status: this.$route.query.dealStatus,
      deal_type: this.$route.query.dealType
      }
    },
     methods: {
       // 流程时候初始化方法
        init(data) {
          console.log(data)
           if (data != null && data.formId !== undefined) {
             this.queryId = data.formId
             this.data = data
             this.showBtn = false
            } else {
                this.$message.error('系统错误')
                return
            }
             getDesignFormData(data.formId).then(response => {
                    this.$refs.vFormRef.resetForm()
                    this.pojo = response.data
                    // 赋值表信息id
                    this.headId = response.data.onlineFormDataId
                    this.formJson = JSON.parse(response.data.desformDataJson)
                    this.$refs.vFormRef.setFormJson(this.formJson)
                   this.requestPojo.postAutoForm = '/head/OnlineApiTest/api/formAdd/' + this.headId
                   if (data.id) {
                      // 查询数据
                    requestAll(`${this.requestPojo.getListDetailsData}${this.headId}/${data.id}`, 'get').then(response => {
                          this.$refs.vFormRef.setFormData(response.data)
                          this.requestPojo.postAutoForm = '/head/OnlineApiTest/api/formEdit/' + this.headId
                          this.loading = false
                          })
                    }
             })
       },
      // 初始化表单设计器
      initFormRender(data) {
               this.loading = true
                if (data != null && data !== '') {
                    getDesignFormData(data).then(response => {
                    // 重置表单
                    this.$refs.vFormRef.resetForm()
                    this.pojo = response.data
                    this.formJson = JSON.parse(response.data.desformDataJson)
                    this.$refs.vFormRef.setFormJson(this.formJson)
                    if (this.rowData == null) {
                      this.requestPojo.postAutoForm = '/head/OnlineApiTest/api/formAdd/' + this.headId
                      this.loading = false
                    }
                    if (this.rowData !== null) {
                  // 查询数据
                  requestAll(`${this.requestPojo.getListDetailsData}${this.headId}/${this.rowData.id}`, 'get').then(response => {
                        this.$refs.vFormRef.setFormData(response.data)
                        this.requestPojo.postAutoForm = '/head/OnlineApiTest/api/formEdit/' + this.headId
                        this.loading = false
                        })
                      }
                  })
                } else {
                  this.$refs.vFormRef.resetForm()
                }
             },
             submitForm() {
                return new Promise((resolve, reject) => {
                 this.$refs.vFormRef.getFormData().then(formData => {
                 // 点击的是编辑
                 if (this.rowData != null) {
                  formData.id = this.rowData.id
                 } else if (this.data.id) {
                     formData.id = this.data.id
                 }
                 // 如果是项目里需增加项目id
                 if (this.pojo.isRelevance === '1') {
                   formData[this.pojo.relevanceField] = this.$route.query.id
                 }
                const subData = JSON.stringify(formData)
                 requestMethod(this.requestPojo.postAutoForm, 'post', subData).then(response => {
                     if (response.code === 200) {
                        this.msgSuccess(this.$t('保存成功'))
                        resolve(response.data)
                        this.closeForm()
                  } else {
                        this.msgError(response.msg)
                   }
                 })
                }).catch(error => {
                console.log(error)
                 reject()
                })
                }).then((Data) => {
                  console.log('promise', Data)
                })
        //        this.$refs.vFormRef.getFormData().then(formData => {
        //          // 点击的是编辑
        //          if (this.rowData != null) {
        //           formData.id = this.rowData.id
        //          }
        //          // 如果是项目里需增加项目id
        //          if (this.pojo.isRelevance === '1') {
        //            formData[this.pojo.relevanceField] = this.$route.query.id
        //          }
        //         const subData = JSON.stringify(formData)
        //          return new Promise((resolve, reject) => {
        //            requestMethod(this.requestPojo.postAutoForm, 'post', subData).then(response => {
        //              if (response.code === 200) {
        //                 this.msgSuccess(this.$t('保存成功'))
        //                  resolve(response.data)
        //           } else {
        //                 this.msgError(response.msg)
        //                 reject()
        //         }
        //         this.loading = false
        //     })
        //  })
        //           // Form Validation OK
        //   //        requestMethod(this.requestPojo.postAutoForm, 'post', subData).then(response => {
        //   //            if (response.code === 200) {
        //   //               this.msgSuccess(this.$t('保存成功'))
        //   //               this.closeForm()
        //   //       } else {
        //   //        this.msgError(response.msg)
        //   //   }
        //   //       this.loading = false
        //   // })
        // }).catch(error => {
        //  console.log(error)
        // })
      },
       // 关闭form页面
      closeForm() {
          // 关闭form页面遮罩层
          this.loading = false
          // 关闭form页面
          if (this.$parent.$parent.formTempletOpen !== undefined) {
            this.$parent.$parent.formTempletOpen = false
          }
          // 刷新list页面
          if (this.$parent.$parent.queryHandle !== undefined) {
            this.$parent.$parent.queryHandle()
          }
      },
      // 初始化一些需要用的数据 用于表单数据 新增或编辑时候
      initialize(data, row) {
        this.headId = data
        this.rowData = row
      }
   }
}

</script>

<style scoped>
.footerBtn {
  text-align: right;
}
</style>
